<?php


namespace SidToken\Model;


use Think\Model;

class SidMinerProductionLogModel extends Model
{
    public function getPoolTotalPruduction()
    {
        $data = S("getPoolTotalPruduction");
        if (!$data) {
            $result = $this->field("IFNULL(SUM(amount),0) total_production")->find();
            $data = $result['total_production'];
            S("getPoolTotalPruduction", $data, 600);
        }
        return $data;
    }

    public function getPoolYesterdayProduction()
    {
        $yesterday = strtotime(date("Ymd")) - 86400;
        $result = $this->field("IFNULL(SUM(amount),0) total_production")->where(['create_time' => $yesterday])->find();
        $data = $result['total_production'];
        return $data;
    }


    public function getTotalPruduction($uid, $minerId)
    {
        $key = "getTotalPruduction_{$uid}_{$minerId}";
        $data = S($key);
        if (!$data) {
            $result = $this->field("IFNULL(SUM(amount),0) total_production")->where(['uid' => $uid, 'miner_id' => $minerId])->find();
            $data = $result['total_production'];
            S($key, $data, 600);
        }
        return $data;
    }

    public function getYesterdayProduction($uid, $minerId)
    {
        $key = "getYesterdayProduction_{$uid}_{$minerId}";
        $data = S($key);
        if (!$data) {
            $yesterday = strtotime(date("Ymd")) - 86400;
            $result = $this->where(['uid' => $uid, 'miner_id' => $minerId, 'create_time' => $yesterday])->getField('amount');
            $data = $result == null ? 0 : $result;
            S($key, $data, 600);
        }
        return $data;
    }

    public function getUserTotalPruduction($uid)
    {
        $key = "getUserTotalPruduction_{$uid}";
        $data = S($key);
        if (!$data) {
            $result = $this->field("IFNULL(SUM(amount),0) total_production")->where(['uid' => $uid])->find();
            $data = $result['total_production'];
            S($key, $data, 600);
        }
        return $data;
    }

    public function getUserYesterdayProduction($uid)
    {
        $key = "getUserYesterdayProduction_{$uid}";
        $data = S($key);
        if (!$data) {
            $yesterday = strtotime(date("Ymd")) - 86400;
            $result = $this->field("IFNULL(SUM(amount),0) total_production")->where(['uid' => $uid, 'create_time' => $yesterday])->find();
            $data = $result['total_production'];
            S($key, $data, 600);
        }
        return $data;
    }

    public function createOrder($uid, $minerId, $amount, $orderSn, $createTime)
    {
        $data = [
            'uid' => $uid,
            'miner_id' => $minerId,
            'amount' => $amount,
            'order_sn' => $orderSn,
            'create_time' => $createTime,
        ];

        $id = $data['id'] = $this->add($data);
        return $data;
    }


}